package com.guofen.service.shlj.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guofen.dao.ShljBlockageCleaningMapper;
import com.guofen.pojo.ShljAbnormalGarbage;
import com.guofen.pojo.ShljBlockageCleaning;
import com.guofen.service.shlj.BlockageCleaningService;
import com.guofen.util.DateAndTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class BlockageCleaningServiceImpl implements BlockageCleaningService {

    @Autowired
    private ShljBlockageCleaningMapper shljBlockageCleaningMapper;

    @Override
    public boolean addForm(String time,String blockage_position,String cleaning_person,String recording_person,String remarks) {
        //获取当前日期
        String date = DateAndTimeUtil.getDate();

        ShljBlockageCleaning shljBlockageCleaning = new ShljBlockageCleaning();
        shljBlockageCleaning.setDate(date);
        shljBlockageCleaning.setTime(time);
        shljBlockageCleaning.setBlockagePosition(blockage_position);
        shljBlockageCleaning.setCleaningPerson(cleaning_person);
        shljBlockageCleaning.setRecordingPerson(recording_person);
        shljBlockageCleaning.setRemarks(remarks);
        int result = shljBlockageCleaningMapper.insert(shljBlockageCleaning);
        if (result == 1)
            return true;
        else
            return false;
    }

    @Override
    public Map<String, Object> get(int pageSize, int currentPage) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        List<ShljBlockageCleaning> list = shljBlockageCleaningMapper.selectAll();
        PageInfo pageRecord = new PageInfo(list);
        long sum = pageRecord.getTotal();
        list = pageRecord.getList();
        map.put("records",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public Map<String, Object> getByTime(int pageSize, int currentPage, long startTime, long endTime) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String startDate = dateFormat.format(new Date(startTime));
        String endDate = dateFormat.format(new Date(endTime));
        List<ShljBlockageCleaning> list = shljBlockageCleaningMapper.selectByDate(startDate, endDate);
        PageInfo pageRecord = new PageInfo(list);
        long sum = pageRecord.getTotal();
        list = pageRecord.getList();
        map.put("records",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public int delete(int id) {
        return shljBlockageCleaningMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int update(ShljBlockageCleaning record) {
        return shljBlockageCleaningMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public List<ShljBlockageCleaning> findForm() {
        return shljBlockageCleaningMapper.findLast();
    }
}
